// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

Vue.use(ElementUI);

Vue.config.productionTip = false

axios.defaults.baseURL = "/api/"

Vue.prototype.$axios = axios

// 用来给axios发送请求时指定默认的请求前缀
// this.$axios.get("abc/aaa")     => http://localhost:9090/abc/aaa
// axios.defaults.baseURL = "http://192.168.5.219:8092/";

// 创建请求对象（axios）
let http = axios.create({});

// 响应拦截器
http.interceptors.response.use(
  response => {
  console.log("响应拦截");
  console.log(response);
  let header=response.headers
  console.log(header.authorization);

  // 判断响应中是否有token信息，如果有则将token放到本地
  let token = header.access_token;
  if (token) {
    window.localStorage.setItem("access_token",res.data.access_token);
    window.localStorage.setItem("scope",res.data.scope);
  }

  let refreshtoken1 = header.refresh_token  ;
  console.log(refreshtoken1);

  if (refreshtoken1) {
    window.localStorage.setItem("refresh_token", refreshtoken1);
  }
  console.log(response.data.code);
  // 判断
  if (response.data.code == 403) {
    // 后台返回没登录
    // 跳转到登录页（路由）
    window.location.href = "/login"
    if (vue.$route.path == "/login") {
      return response;
    } else {
      vue.$router.push("/login");
      return;
    }
  }

  // 判断返回的是否是没权限，是则进行提示
  if (response.data.code == 401) {
    vue.$message.error(response.data.message);
    return;
  }

  return response;
});
// 请求拦截：目的是为了自动给axios发送的请求携带上token
http.interceptors.request.use(config => {
  console.log(config);
  // 得到token  本地
  let access_token= window.localStorage.getItem("access_token");
  let  refresh_token = window.localStorage.getItem("refresh_token");
  let  uid = window.localStorage.getItem("uid");
if (config.url.startsWith("oauth/token?grant_type=password")) {
  console.log("aa");


}else{
  config.headers.access_token = access_token;
  config.headers.refresh_token = refresh_token;
 // config.headers.uid = uid;
  config.headers.authorization="Bearer " + access_token;
  console.log(config.headers);


}




  // 放行请求
  return config;
});






// 将axios设置成vue的属性
Vue.prototype.$axios = http;

Vue.prototype.$url = "http://192.168.5.199:8092/";

Vue.config.productionTip = false;

/* eslint-disable no-new */
let vue = new Vue({
  el: "#app",
  router,
  components: { App },
  template: "<App/>"
});
